package com.spider.admin.store.primary.mapper;

import com.spider.admin.store.primary.model.ServiceInfo;
import com.spider.admin.store.primary.model.TenantAndAppInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface TenantAppTreeMapper {

    @Select({"<script>",
            "SELECT pid,pid node_id,app_name node_name,tenant_id parent_id,'app' node_type FROM t_application_info",
            "<if test='tenantId != null'> WHERE tenant_id = #{tenantId}</if>",
            " UNION ALL ",
            "SELECT tenant_id pid,tenant_id node_id,tenant_name node_name,'root' parent_id,'tenant' node_type FROM t_account_info",
            "<if test='tenantId != null'> WHERE tenant_id = #{tenantId}</if>",
            "</script>"})
    @Results(value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "node_id",property = "nodeId"),
            @Result(column = "node_name",property = "nodeName"),
            @Result(column = "parent_id",property = "parentId"),
            @Result(column = "node_type",property = "nodeType")
    })
    List<TenantAndAppInfo> getAllInfo(String tenantId);

    @Select({"<script>",
            "Select sm.*, ap.v_pid   from t_service_marks sm LEFT JOIN (select * from t_account_service_permissions WHERE account=#{account}) ap ON sm.pid=ap.service_id  WHERE 1=1",
            "<if test='appId != null'> AND app_id = #{appId}</if>",
            "<if test='tenantId != null'> AND tenant_id = #{tenantId}</if>",
            "</script>"})
    @Results(value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "service_name", property = "serviceName"),
            @Result(column = "service_desc", property = "serviceDesc"),
            @Result(column = "app_id", property = "appId"),
            @Result(column = "service_url", property = "serviceUrl"),
            @Result(column = "publish_client_id", property = "publishClientId"),
            @Result(column = "line_status", property = "lineStatus"),
            @Result(column = "tenant_id", property = "tenantId"),
            @Result(column = "v_pid", property = "vpid"),
            @Result(property = "appInfo", column = "app_id",
                    one = @One(select = "com.spider.admin.store.primary.mapper.AppMapper.queryAppById", fetchType = FetchType.EAGER)),
            @Result(column = "service_doc_id", property = "serviceDocId")
    })
    List<ServiceInfo> getServiceList(String account,String appId, String tenantId);

    @Select({"<script>",
            "Select sm.*, ap.v_pid   from t_service_marks sm , (select * from t_account_service_permissions WHERE account=#{account}) ap WHERE sm.pid=ap.service_id ",
            "<if test='appId != null'> AND app_id = #{appId}</if>",
            "<if test='tenantId != null'> AND tenant_id = #{tenantId}</if>",
            "</script>"})
    @Results(value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "service_name", property = "serviceName"),
            @Result(column = "service_desc", property = "serviceDesc"),
            @Result(column = "app_id", property = "appId"),
            @Result(column = "service_url", property = "serviceUrl"),
            @Result(column = "publish_client_id", property = "publishClientId"),
            @Result(column = "line_status", property = "lineStatus"),
            @Result(column = "tenant_id", property = "tenantId"),
            @Result(column = "v_pid", property = "vpid"),
            @Result(property = "appInfo", column = "app_id",
                    one = @One(select = "com.spider.admin.store.primary.mapper.AppMapper.queryAppById", fetchType = FetchType.EAGER)),
            @Result(column = "service_doc_id", property = "serviceDocId")
    })
    List<ServiceInfo> getAccountServiceList(String account,String appId, String tenantId);
}
